/**
 * Created by zhoua on 2017/8/8.
 */
import axios from 'axios'
var baseURL = '/amdms/api/'
import { Message } from 'element-ui'
import { getToken,removeToken } from '@/utils/auth'
import router from '@/router'

if (process.env.NODE_ENV === 'production') {
  baseURL
} else {
  baseURL
}

export function fetch(options, json) {
  return new Promise((resolve, reject) => {
    const instance = axios.create({
      timeout: 20000,
      baseURL,
      headers: json ? { 'Content-Type': 'application/x-www-from-urlencoded;charset=utf-8' } : { 'Content-Type': 'application/json' }
    })
    instance(options)
      .then(response => {
        var res = response.data
        if (+res.state === 0) {
          resolve(res)
          Message({
            message: res.friendlyMsg,
            type: 'success',
            duration: 2000
          })
        } else {
          if (+res.code === 101) {
            removeToken()
            router.push({ path: '/login' })
          } else if (+res.code === 104) {
            Message({
              message: '填写数据有误，请检查数据',
              type: 'error',
              duration: 2000
            })
            resolve(res)
            return
          } else if (+res.code === -1) {
            Message({
              message: '数据有误，请联系管理员',
              type: 'error',
              duration: 2000
            })
            resolve(res)
            return
          } else {
            Message({
              message: res.friendlyMsg,
              type: 'error',
              duration: 2000
            })
            resolve(res)
            return
          }
          reject(res)
        }
      })
      .catch(error => {
        if (!error) return
        reject(error)
        // console.log('服务器内部错误')
      })
  })
}

export function fetchToken(options, json) {
  return new Promise((resolve, reject) => {
    const instance = axios.create({
      timeout: 20000,
      baseURL,
      headers: json ? { 'Content-Type': 'application/x-www-from-urlencoded;charset=utf-8', 'token': getToken() } : { 'Content-Type': 'application/json', 'token': getToken() }
    })
    instance(options)
      .then(response => {
        var res = response.data
        if (+res.state === 0) {
          resolve(res)
          Message({
            message: res.friendlyMsg,
            type: 'success',
            duration: 2000
          })
        } else {
          if (+res.code === 101) {
            removeToken()
            router.push({ path: '/login' })
          } else if (+res.code === 104) {
            Message({
              message: '填写数据有误，请检查数据',
              type: 'error',
              duration: 2000
            })
            resolve(res)
            return
          } else if (+res.code === -1) {
            Message({
              message: '数据有误，请联系管理员',
              type: 'error',
              duration: 2000
            })
            resolve(res)
            return
          } else {
            Message({
              message: res.friendlyMsg,
              type: 'error',
              duration: 2000
            })
            resolve(res)
            return
          }
          reject(res)
        }
      })
      .catch(error => {
        Message({
          message: '数据有误，请联系管理员',
          type: 'error',
          duration: 2000
        })
        if (!error) return
        reject(error)
      })
  })
}

export function fetchNoMsg(options, json) {
  return new Promise((resolve, reject) => {
    const instance = axios.create({
      timeout: 20000,
      baseURL,
      headers: json ? { 'Content-Type': 'application/x-www-from-urlencoded;charset=utf-8', 'token': getToken() } : { 'Content-Type': 'application/json', 'token': getToken() }
    })
    instance(options)
      .then(response => {
        var res = response.data
        if (+res.state === 0) {
          resolve(res)
        } else {
          if (+res.code === 101) {
            removeToken()
            router.push({ path: '/login' })
          } else if (+res.code === 104) {
            Message({
              message: '填写数据有误，请检查数据',
              type: 'error',
              duration: 2000
            })
            resolve(res)
            return
          } else if (+res.code === -1) {
            Message({
              message: '数据有误，请联系管理员',
              type: 'error',
              duration: 2000
            })
            resolve(res)
            return
          } else {
            Message({
              message: res.friendlyMsg,
              type: 'error',
              duration: 2000
            })
            resolve(res)
            return
          }
          reject(res)
        }
      }).catch(error => {
        Message({
          message: '数据有误，请联系管理员',
          type: 'error',
          duration: 2000
        })
        if (!error) return
        reject(error)
      })
  })
}

export function fetchForLocation(options, json) {
  return new Promise((resolve, reject) => {
    const instance = axios.create({
      timeout: 20000,
      baseURL,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded','token': getToken()
      },
      withCredentials: true
    })
    instance(options)
      .then(response => {
        resolve(response)
      })
      .catch(error => {
        if (!error) return
        reject(error)
      })
  })
}

export function fetchForImg(options, json) {
  return new Promise((resolve, reject) => {
    const instance = axios.create({
      timeout: 20000,
      baseURL,
      headers: {
        'Content-Type': 'multipart/form-data','token': getToken()
      }
    })
    instance(options)
      .then(response => {
        var res = response.data
        if (+res.code === 0) {
          resolve(res)
          // console.log(res.friendlyMsg)
        } else {
          if (res.code) {
            // console.log(res.friendlyMsg)
          } else {
            // console.log('服务器错误')
          }
          reject(res)
        }
      }).catch(error => {
        Message({
          message: error.friendlyMsg,
          type: 'error',
          duration: 2000
        })
        if (!error) return
        reject(error)
        // console.log('服务器内部错误')
      })
  })
}
